package com.pds.web.testOrganization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.web.device.entity.PdsTestOrganizationDevice;
import com.pds.web.device.mapper.PdsTestOrganizationDeviceMapper;
import com.pds.web.testOrganization.domain.*;
import com.pds.web.testOrganization.mapper.PdsTestOrganizationHonorMapper;
import com.pds.web.testOrganization.mapper.PdsTestOrganizationInspectMapper;
import com.pds.web.testOrganization.mapper.PdsTestOrganizationQualificationMapper;
import com.pds.web.testOrganization.mapper.PdsTestOrganizationVOMapper;
import com.pds.web.testOrganization.service.IPdsTestOrganizationVOService;
import com.pds.web.testOrganizationInfo.domain.PdsTestOrganizationInfo;
import com.pds.web.testOrganizationInfo.domain.PdsTestOrganizationPerson;
import com.pds.web.testOrganizationInfo.domain.PdsTestOrganizationPlace;
import com.pds.web.testOrganizationInfo.mapper.PdsTestOrganizationPersonMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service("pdsTestOrganizationVOService")
public class PdsTestOrganizationVOServiceImpl extends ServiceImpl<PdsTestOrganizationVOMapper, PdsTestOrganizationVO> implements IPdsTestOrganizationVOService {

    @Resource
    private PdsTestOrganizationVOMapper pdsTestOrganizationVOMapper;

    @Resource
    private PdsTestOrganizationDeviceMapper pdsTestOrganizationDeviceMapper;


//    @Autowired
//    private PdsTestOrganizationVO pdsTestOrganizationVO;

//    @Override
//    public PdsTestOrganizationVO listById(Long organizationId) {
//        List<PdsTestOrganizationHonor> honors = pdsTestOrganizationVOMapper.listHonor(organizationId);
//        List<PdsTestOrganizationInspect> inspects = pdsTestOrganizationVOMapper.listInspect(organizationId);
//        List<PdsTestOrganizationQualification> qualifications = pdsTestOrganizationVOMapper.listQualification(organizationId);
//        List<PdsTestOrganizationPerson> people = pdsTestOrganizationVOMapper.listPerson(organizationId);
//        List<PdsTestOrganizationPlace> places = pdsTestOrganizationVOMapper.listPlace(organizationId);
//
//        pdsTestOrganizationVO.setHonors(honors);
//        pdsTestOrganizationVO.setInspects(inspects);
//        pdsTestOrganizationVO.setQualifications(qualifications);
//        pdsTestOrganizationVO.setPeople(people);
//        pdsTestOrganizationVO.setPlaces(places);
//        return pdsTestOrganizationVO;
//    }

//    @Override
//    public Boolean updateHonorStatus(OrganizationStatusDTO statusDTO) {
//        return pdsTestOrganizationVOMapper.updateHonorStatus(statusDTO) > 0;
//    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public Boolean updateInfoStatus(OrganizationStatusDTO statusDTO) {
        boolean flag1 = pdsTestOrganizationVOMapper.updateHonorStatus(statusDTO) > 0;
        boolean flag2 = pdsTestOrganizationVOMapper.updateInspectStatus(statusDTO) > 0;
        boolean flag3 = pdsTestOrganizationVOMapper.updateQualificationStatus(statusDTO) > 0;
        boolean flag4 = pdsTestOrganizationVOMapper.updatePersonStatus(statusDTO) > 0;
        boolean flag5 = pdsTestOrganizationVOMapper.updatePlaceStatus(statusDTO) > 0;
        boolean flag6 = pdsTestOrganizationVOMapper.updateInfoStatus(statusDTO) > 0;
        return flag1 & flag2 & flag3 & flag4 & flag5 & flag6;
    }

//    @Override
//    public Boolean updateInspectStatus(OrganizationStatusDTO statusDTO) {
//        return pdsTestOrganizationVOMapper.updateInspectStatus(statusDTO) > 0;
//    }
//
//    @Override
//    public Boolean updateQualificationStatus(OrganizationStatusDTO statusDTO) {
//        return pdsTestOrganizationVOMapper.updateQualificationStatus(statusDTO) > 0;
//    }
//
//    @Override
//    public Boolean updatePersonStatus(OrganizationStatusDTO statusDTO) {
//        return pdsTestOrganizationVOMapper.updatePersonStatus(statusDTO) > 0;
//    }
//
//    @Override
//    public Boolean updatePlaceStatus(OrganizationStatusDTO statusDTO) {
//        return pdsTestOrganizationVOMapper.updatePlaceStatus(statusDTO) > 0;
//    }

    @Override
    public List<PdsTestOrganizationVO> detailList() {
//        List<PdsTestOrganizationVO> list = pdsTestOrganizationVOMapper.detailList();
//        return list;
        List<PdsTestOrganizationVO> pdsTestOrganizationVOList = new ArrayList<>();
        List<PdsTestOrganizationInfo> pdsTestOrganizationInfoList = pdsTestOrganizationVOMapper.selectInfoList();
        pdsTestOrganizationInfoList.stream().forEach(infoList -> {
            Long organizationId = infoList.getId();
            List<PdsTestOrganizationHonor> honors = pdsTestOrganizationVOMapper.listHonor(organizationId);
            List<PdsTestOrganizationInspect> inspects = pdsTestOrganizationVOMapper.listInspect(organizationId);
            List<PdsTestOrganizationQualification> qualifications = pdsTestOrganizationVOMapper.listQualification(organizationId);
            List<PdsTestOrganizationPerson> people = pdsTestOrganizationVOMapper.listPerson(organizationId);
            List<PdsTestOrganizationPlace> places = pdsTestOrganizationVOMapper.listPlace(organizationId);
            QueryWrapper<PdsTestOrganizationDevice> qw = new QueryWrapper<>();
            List<PdsTestOrganizationDevice> devices = pdsTestOrganizationDeviceMapper.selectList(qw.eq("organization_id", organizationId));
            PdsTestOrganizationVO pdsTestOrganizationVO = new PdsTestOrganizationVO();
            BeanUtils.copyProperties(infoList, pdsTestOrganizationVO);
            pdsTestOrganizationVO.setHonors(honors);
            pdsTestOrganizationVO.setInspects(inspects);
            pdsTestOrganizationVO.setQualifications(qualifications);
            pdsTestOrganizationVO.setPeople(people);
            pdsTestOrganizationVO.setPlaces(places);
            pdsTestOrganizationVO.setDevices(devices);
            pdsTestOrganizationVOList.add(pdsTestOrganizationVO);
        });
        return pdsTestOrganizationVOList;
    }

    @Override
    public int getTotal() {
        int total = pdsTestOrganizationVOMapper.getTotal();
        return total;
    }

//    @Override
//    public List<PdsTestOrganizationInfo> selectInfoList(PdsTestOrganizationInfo pdsTestOrganizationInfo) {
//        List<PdsTestOrganizationInfo> pdsTestOrganizationInfoList = pdsTestOrganizationVOMapper.selectInfoList(pdsTestOrganizationInfo);
//        return pdsTestOrganizationInfoList;
//    }


//    @Override
//    public List<PdsTestOrganizationVO> selectVOlist() {
//
//    }
}
